﻿using UnityEngine;
using System.Collections;
using System;

namespace Battle
{
    public class UnionFightBattleOver : IBattleOver
    {
        public BattleType GetBattleType()
        {
            return BattleType.UNION_FIGHT;
        }

        bool localResult;
        bool remoteResult;
        OnBattleResult onResult = null;

        public UnionFightBattleOver(OnBattleResult onResult)
        {
            this.onResult = onResult;
        }

       
        public void Excute()
        {
            localResult = BattleManager.Instance.battleViewCtrl.winCamp == BattleCamp.Attacker;
            remoteResult = false;
            UnionFightFightResult();
            End();
        }

        void UnionFightFightResult()
        {
            var msg = StaticData.lastBattleEndMsg as ProtoBuf.Message.SCFriendFight;
            var winPlayerUID = localResult ? StaticData.curBattleData.attackFighters[0].role.playerUID :
                StaticData.curBattleData.defendFighters[0].role.playerUID;
            if (winPlayerUID != msg.winPlayerId)
            {
                remoteResult = !localResult;
            }
            else
            {
                remoteResult = localResult;
            }
            StaticData.isLastBattleWin = remoteResult;
        }

        public void End()
        {
            if (onResult != null)
            {
                // 切磋已客户端结果为准
                onResult(localResult);
            }
        }

    }
}
